﻿using System;
using System.Collections.Generic;
using System.Text;

namespace SqlPrompt
{
	public class DateUtil
	{
		public static string GetTimeSpanStringFromTicks(long ticks)
		{
			TimeSpan timeSpan = new TimeSpan(ticks);

			var parts = new List<string>();

			if (timeSpan.Days > 0)
				parts.Add(timeSpan.Days + StringUtil.Plural(" day", timeSpan.Days));

			if (timeSpan.Hours > 0)
				parts.Add(timeSpan.Hours + StringUtil.Plural(" hour", timeSpan.Hours));

			if (timeSpan.Minutes > 0)
				parts.Add(timeSpan.Minutes + StringUtil.Plural(" minute", timeSpan.Minutes));

			if (timeSpan.Seconds > 0)
				parts.Add(timeSpan.Seconds + StringUtil.Plural(" second", timeSpan.Seconds));

			if (parts.Count == 0)
				parts.Add(timeSpan.Milliseconds + StringUtil.Plural(" millisecond", timeSpan.Milliseconds));

			string output = "";
			string sep = "";
			for (int i = 0; i < parts.Count; i++)
			{
				output += sep + parts[i];
				sep = i == parts.Count - 2 ? " and " : ", ";
			}

			return output;
		}
	}
}
